$(function () {
    //点赞和取消点赞的方法
    var time = 0;
    $('.praise').click(function () {
        //判断计时器是否处于关闭状态
        if (time == 0) {
            time = 2; //设定间隔时间（秒）
            //启动计时器，倒计时time秒后自动关闭计时器。
            var index = setInterval(function () {
                time--;
                if (time == 0) {
                    clearInterval(index);
                }
            }, 2000);

        } else {
            $(document).dialog({
                type: 'notice',
                infoText: '请稍后再赞',
                autoClose: 1000,
                position: 'bottom'  // center: 居中; bottom: 底部
            });
        }
    });

    $(".praise").click(function () {
        var user=JSON.parse(sessionStorage.getItem("user"));
        if(user){
            var userId =user.id;
            var praise = $(this);
            var praiseCount = $(this).find("span");
            console.log(praiseCount.data("did"));
            /*被点赞者的id*/
            var up = $(this).find("span").data("did");
            //点赞数
            var num = 0;
            //  var  color= $(this).data("up");
            var color = praise.attr("data-up");
            console.log(praise.get(0));
            console.log(color);
            /*被点赞文章的id*/
            var atId = $(this).data("did");
            if (color == 1) {
                //取消
                $.ajax({
                    /* /{up.id}/{user.id}/{state}/{atId}/{stateUp}/thumbsUps"*/
                    url: "/users/" + userId + "/" + up + "/1/" + atId + "/1/thumbsUps",
                    method: 'delete',
                    success: function (data) {
                        num = data - 1;
                        praiseCount.html(num);
                        praise.attr("data-up", 2);
                        praise.css("color", "");
                    }
                });
            }
            if (color == 2) {
                //点赞
                $.post("/users/" + userId + "/" + up + "/thumbsUps", {
                    //文章的章台
                    state: 1,
                    //文章的id
                    articleId: atId,
                    stateUp: 1
                }, function (data) {
                    num = data + 1;
                    praiseCount.html(num);
                    praise.css("color", "red");
                    praise.attr("data-up", 1);

                });
            }
        }else {
            location.href="/login.html"
        }
    });
});
